﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using DRHotel_v1.Classes;
using DRHotel_v1.Classes.DBManagers;

namespace DRHotel_v1.RoomAvaAndBooking
{
    public partial class CustomerCreateFrm : Form
    {
        public CustomerCreateFrm()
        {
            InitializeComponent();
        }

        private void CustomerCreateFrm_Load(object sender, EventArgs e)
        {

        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (tbxICNo.Text.Length == 0 || cbbICType.Text.Length == 0 || tbxFirstName.Text.Length == 0 || tbxLastName.Text.Length == 0)
            {
                MessageBox.Show("Some necessary information is not filled!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (!IsValidEmailAddress(tbxEmail.Text))
            {
                MessageBox.Show("Invalid Email Address!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxEmail.SelectAll();
                tbxEmail.Focus();
                return;
            }
            Customer cus = new Customer();
            cus.cusIC = tbxICNo.Text;
            cus.ICType = cbbICType.Text;
            cus.firstName = tbxFirstName.Text;
            cus.lastName = tbxLastName.Text;
            if (rbnMale.Checked == true)
            {
                cus.gender = true;
            }
            else
            {
                cus.gender = false;
            }
            cus.dob = dateTimePicker1.Value;
            cus.country = tbxCountry.Text;
            cus.phoneNo = tbxPhone.Text;
            cus.email = tbxEmail.Text;
            cus.mailingAddress = tbxAddress.Text;
            cus.postCode = tbxPostal.Text;
            cus.cusType = cbbCustomerType.Text;
            if (RoomAvaAndBookingDBManager.SearchExistingCus(tbxICNo.Text) == true)
            {
                MessageBox.Show("This customer is existing!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (RoomAvaAndBookingDBManager.InsertCustomer(cus) != 0)
            {
                MessageBox.Show("Customer information updated successful!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                MessageBox.Show("Cannot update customer information!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            if (RoomAvaAndBookingDBManager.SearchExistingCus(tbxICNo.Text)==true)
            {
                MessageBox.Show("This customer is existing!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                MessageBox.Show("Valid customer!", "Message", MessageBoxButtons.OK);
                return;
            }
        }

        private void tbxICNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            Regex regex = new Regex("^[A-Z0-9]*$");
            if (regex.IsMatch(tbxICNo.Text))
            {
                errorProvider1.SetError(tbxICNo, String.Empty);
            }
            else
            {
                errorProvider1.SetError(tbxICNo, "Only capital letters and numbers may be entered here");
            }
        }
        public bool IsValidEmailAddress(string email)//validate email address
        {
            try
            {
                MailAddress ma = new MailAddress(email);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void tbxPhone_TextChanged(object sender, EventArgs e)
        {
            Regex regex = new Regex("^[0-9]+$");
            if (regex.IsMatch(tbxICNo.Text))
            {
                errorProvider1.SetError(tbxPhone, String.Empty);
            }
            else
            {
                errorProvider1.SetError(tbxPhone, "Only numbers may be entered here");
            }
        }

        private void tbxPostal_TextChanged(object sender, EventArgs e)
        {
            Regex regex = new Regex("^[A-Z0-9]*$");
            if (regex.IsMatch(tbxICNo.Text))
            {
                errorProvider1.SetError(tbxPostal, String.Empty);
            }
            else
            {
                errorProvider1.SetError(tbxPostal, "Only capital letters and numbers may be entered here");
            }
        }
        
    }
}
